package com.xinzhitong.www.service.impl;

import com.xinzhitong.www.exception.visitor.NullNewsOutlineException;
import com.xinzhitong.www.exception.visitor.NullProfessionInformationException;
import com.xinzhitong.www.mapper.VisitorMapper;
import com.xinzhitong.www.pojo.News;
import com.xinzhitong.www.pojo.Profession;
import com.xinzhitong.www.service.VisitorService;
import com.xinzhitong.www.utils.FileHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游客服务实现
 *
 * @author 魏骆淳
 */
@Service
@Transactional
public class VisitorServiceImpl implements VisitorService {

    @Resource
    private VisitorMapper visitorMapper;

    @Override
    public Map<String, Object> getProfessionInformation() throws NullProfessionInformationException {
        List<Profession> professions = visitorMapper.selectProfessionInformation();
        if (professions.size() == 0) {
            throw new NullProfessionInformationException();
        } else {
            List<Object> professionInformation = new ArrayList<>();
            for (Profession profession : professions) {
                Map<String, Object> profession1 = new HashMap<>();
                profession1.put("professionId", profession.getId());
                profession1.put("professionName", profession.getName());
                professionInformation.add(profession1);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("professionInformation", professionInformation);
            return map;
        }
    }

    @Override
    public Map<String, Object> getNewsOutline() throws NullNewsOutlineException {
        List<News> news = visitorMapper.selectNewsOutline();
        Integer count = visitorMapper.selectCountNews();
        if (news.size() == 0 || count == null) {
            throw new NullNewsOutlineException();
        } else {
            List<Object> newsOutline = new ArrayList<>();
            for (News news1 : news) {
                Map<String, Object> news2 = new HashMap<>();
                news2.put("newsId", news1.getId());
                news2.put("newsTitle", news1.getTitle());
                news2.put("newsPicture", FileHelper.fileURL + news1.getPicture());
                newsOutline.add(news2);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("newsOutline", newsOutline);
            map.put("newsCount", count);
            return map;
        }
    }

}
